<?php
class MainController extends AppController {

    public $layout = 'main';

    function beforeFilter() {
        parent::beforeFilter();
        $this->set('nav','index');  
    }

    function index(){   
        $params = $this->request->data;
        if($this->request->is('post') && $params['dosubmit']==1){ 
            if(empty($params['u_mobile'])){
                $this->ajaxReturn(4001, '手机号不能为空!');  
            }
            if(!Ivf::isMobile($params['u_mobile']))
                $this->ajaxReturn(4002, '请输入有效的11位手机号码!');   

            $this->loadModel('UsersAppoint');  
    		$dataList = $this->UsersAppoint->find('first',array('conditions'=>array('status'=>0,'mobile'=>$params['u_mobile'],'source'=>'sougou91-首页','web_site'=>1)));
            if(!empty($dataList))
                $this->ajaxReturn(4003, '不能重复提交！'); 
  
            $db_data = array();
            $db_data['types'] =  isset($params['symptoms'])?intval($params['symptoms']):0;
            $db_data['user_name'] =  $params['u_name'];
            $db_data['mobile'] =  $params['u_mobile'];
            $db_data['system'] = Ivf::getOSInfo(strtolower($_SERVER['HTTP_USER_AGENT']));
            $db_data['brand'] = Ivf::getOSBrand(strtolower($_SERVER['HTTP_USER_AGENT'])); 
            $db_data['source'] = "sougou91-首页";
            $db_data['web_site'] = 1;
            $db_data['status'] = 0; 
            $db_data['ip'] = Ivf::getClientIp();; 
            $db_data['create_time'] = date("Y-m-d H:i:s",time()); 
            if(!$this->UsersAppoint->save($db_data)) 
                    $this->ajaxReturn(4005, '预约失败！'); 
              
            $this->ajaxReturn(200, '申请成功！');   
        } 
        $this->loadModel('New'); 
        $this->loadModel('Share');  
        $this->loadModel('User');  
        $orderby=[];  
        $orderby['sort']="desc";
        $orderby['create_time']="desc";
        $conditions['status'] = 1;
        $conditions['is_home'] = 1;
        $conditions['category_id'] = 4;   //泰国试管报道分类id，可修改
        $top4List =$shareList=$top6List=[]; 
        $top4List = $this->New->find('all',array(
            'conditions' => $conditions, 
            'limit' => 4,
            'order' => $orderby,
            )
        );
 

        $conditions['category_id'] = 5;    //最新资讯分类id，可修改 
        $top6List = $this->New->find('all',array(
            'conditions' => $conditions, 
            'limit' => 6,
            'order' => $orderby,
            )
        );

        //分享说，按评论数最多的显示
        $conditions= $orderby=[];  
        $orderby['comment_num']="desc";
        $conditions['status'] = 1;   
        $conditions['type'] = 0;   
        $shareList = $this->Share->find('all',array(
            'conditions' => $conditions, 
            'limit' => 3,
            'order' => $orderby,
            )
        );

        if(!empty($shareList)){
            foreach ($shareList as $key => $value) {   
                 $str= preg_replace('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i',"",$value['Share']['content']); 
                 $userInfo=$this->User->findById(intval($value['Share']['uid'])); 
                 $shareList[$key]['Share']['username']=isset($userInfo['User']['nick_name'])?$userInfo['User']['nick_name']:"保密"; 
                 $shareList[$key]['Share']['content']=Ivf::getSubstr(str_replace('　　', '', $str),0,20);  
                 $shareList[$key]['Share']['avatar']=$userInfo['User']['avatar'];
            }
        }  

        $this->loadModel('Keyword');  
        $keyTopic=Configure::read('KEYWORDS_TYPE');
        $keysInfo = $this->Keyword->find('first',array('conditions'=>array('key'=>$keyTopic['首页']),'order'=>array('id'=>'desc'))); 
        $randNum=Ivf::aideCount();
        $this->set('keysInfo',$keysInfo);
        $this->set('top6List',$top6List);
        $this->set('top4List',$top4List);
        $this->set('randNum',$randNum);  
        $this->set('shareList',$shareList);
        $this->set('disease',Configure::read('DISEASE_TOPIC'));  
    }

}